package modules.majiang.viewManager
{
	/**
	 *
	 * @author xumin.xu 2013-1-25
	 */

	import com.netease.protobuf.UInt64;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import modules.GameEvent;
	import modules.ViewManager;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablesDataManager;
	import modules.majiang.view.TablePotCard;
	
	import utils.SmallCardFactory;

	public class CardPotViewManager extends ViewManager
	{
		private var tableId:UInt64;
		
		private var allcards:Array;

		/**
		 * 麻将层数
		 */
		public static const LEVEL:int = 3;

		/**
		 * 麻将行数
		 */
		public static const ROWS:int = 4;

		/**
		 * 麻将列数
		 */
		public static const COLS:int = 13;

		/**
		 *	第一层麻将的Y左边
		 **/
		public static var downLayerY:int = 5;

		/**
		 *	第二层麻将的Y左边
		 **/
		public static var upLayerY:int = -5;

		/**
		 * 一层麻将的数量
		 **/
		public static var totalNum:int = ROWS * COLS;

		public function get tableDataModel():TableDataModel
		{
			return TablesDataManager.tableDataModeDic[tableId.toString()];
		}
		
		/**
		 * 桌子上的牌
		 **/
		private function get tilesOnTable():Array
		{
			return tableDataModel.tilesOnTable;
		}

		public function CardPotViewManager(tableId:UInt64,mainView:MovieClip)
		{
			super();
			this.tableId = tableId;

			this.mainUI = mainView;

			init();

			mainView.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			mainView.addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage);
		}

		private function onAddToStage(event:Event):void
		{
			//initCardData();
			//更新麻将桌子界面中显示
			dispatcher.addEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, function(event:MajiangEvent):void
			{
				if (tableId.toString() != event.data.tableId.toString())
					return;
				//更新桌子上的牌
				updateCardPot();
			});

			//出牌动画结束
			dispatcher.addEventListener(MajiangEvent.DISCARD_EFFECT_FINISH, function(evnt:Event):void
			{
				//更新麻将牌的显示
				updateCardPot();
			});

			//比赛结束10后清空桌面
			dispatcher.addEventListener(MajiangEvent.CLEAR_TABLE_INFO, function(event:GameEvent):void
			{
				if(tableId.toString() != event.data) return;
				//清空桌子
				reset();
			});
			dispatcher.addEventListener(MajiangEvent.GET_CARDPOT_CARD, onGetCardPotCard);

		}

		private function removeFromStage(event:Event):void
		{

		}

		private function init():void
		{
			allcards = [];
			var tableCard:TablePotCard;
			for (var i:int = 0; i < LEVEL; i++)
			{
				for (var j:int = 0; j < ROWS; j++)
				{
					for (var k:int = 0; k < COLS; k++)
					{
						tableCard = new TablePotCard();
						tableCard.x = i * 3 + k * tableCard.width;
						tableCard.y = -i * 3 + j * tableCard.height;
						tableCard.visible = false;
						mainUI.addChild(tableCard);

						allcards[i * ROWS * COLS + j * COLS + k] = tableCard;
					}
				}
			}
		}

		/**
		 * 刷新桌面上的牌
		 **/
		private function updateCardPot():void
		{
			var tableCard:TablePotCard;
			var tile:Object;
			for (var i:int = 0; i < tilesOnTable.length; i++)
			{
				tableCard = allcards[i];
				tile = tilesOnTable[i];
				tableCard.setFront(SmallCardFactory.getNormalCardBitmapData(tile.type, tile.value));

				tableCard.visible = true;
			}
		}

		private function reset():void
		{
			for (var i:int = 0; i < allcards.length; i++)
			{
				allcards[i].visible = false;
			}
		}

		private function onGetCardPotCard(event:MajiangEvent):void
		{
			if (allcards[event.data.index])
			{
				event.data.targetCard = allcards[event.data.index];
			}
		}
	}
}
